﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MementoWeb.Data;
using MementoLibrary.DTOs;
using AutoPoco.Engine;
using AutoPoco;
using DefaultConnection;

namespace MementoWeb.Tests.Integration
{
    [TestClass]
    public class DataRepositoryTest
    {
        private IGenerationSessionFactory _factory = null;

        public DataRepositoryTest()
        {
            AutomapperConfig.RegisterAutomapperMaps();
            _factory = AutoPocoContainer.Configure(x =>
            {
                x.Conventions(c =>
                {
                    c.Register<SqlDatePropertyConvention>();
                    c.UseDefaultConventions();
                });
                x.AddFromAssemblyContainingType<WorkLogDTO>();
                x.AddFromAssemblyContainingType<WorkLog>();
            });
        }
        [TestMethod]
        public void CreateOrUpdateProject_CreatesProject_IfProjectSourceSystemIsNotPresentInDatabase()
        {
            AutomapperConfig.RegisterAutomapperMaps();
            var session = _factory.CreateSession();
            var projectDTO = session.Single<ProjectDTO>().Get();
            projectDTO.SourceSystem = Guid.NewGuid().ToString();
            var dataRepository = new DataRepository();
            var oldProjectCount = dataRepository.GetAllProjects().Count();
            dataRepository.CreateOrUpdateProject(projectDTO);
            Assert.AreEqual(oldProjectCount + 1, dataRepository.GetAllProjects().Count());
        }

        [TestMethod]
        public void CreateOrUpdateProject_UpdatesProject_IfProjectSourceSystemIsPresentInDatabase()
        {
            AutomapperConfig.RegisterAutomapperMaps();
            var session = _factory.CreateSession();
            var projectDTO = session.Single<ProjectDTO>().Get();
            projectDTO.SourceSystem = Guid.NewGuid().ToString();
            var dataRepository = new DataRepository();
            var oldProjectCount = dataRepository.GetAllProjects().Count();
            dataRepository.CreateOrUpdateProject(projectDTO);
            Assert.AreEqual(oldProjectCount + 1, dataRepository.GetAllProjects().Count());
            dataRepository.CreateOrUpdateProject(projectDTO);
            Assert.AreEqual(oldProjectCount + 1, dataRepository.GetAllProjects().Count());
        }

        [TestMethod]
        public void CreateOrUpdateTask_CreatesTaskWithProjectId_IfTaskSourceSystemIsNotPresentInDatabase()
        {
            AutomapperConfig.RegisterAutomapperMaps();
            var session = _factory.CreateSession();
            var project = session.Single<Project>().Get();
            project.Insert();
            var taskDTO = session.Single<TaskDTO>().Get();
            taskDTO.SourceSystem = Guid.NewGuid().ToString();
            var dataRepository = new DataRepository();
            var oldTaskCount = dataRepository.GetAllTasks().Count();
            dataRepository.CreateOrUpdateTask(taskDTO, project.ProjectId);
            Assert.AreEqual(oldTaskCount + 1, dataRepository.GetAllTasks().Count());
        }

        [TestMethod]
        public void CreateOrUpdateTask_UpdatesTaskWithProjectId_IfTaskSourceSystemIsPresentInDatabase()
        {
            
            AutomapperConfig.RegisterAutomapperMaps();
            var session = _factory.CreateSession();
            var project = session.Single<Project>().Get();
            project.Insert();
            var taskDTO = session.Single<TaskDTO>().Get();
            taskDTO.SourceSystem = Guid.NewGuid().ToString();
            var dataRepository = new DataRepository();
            var oldTaskCount = dataRepository.GetAllTasks().Count();
            dataRepository.CreateOrUpdateTask(taskDTO, project.ProjectId);
            Assert.AreEqual(oldTaskCount + 1, dataRepository.GetAllTasks().Count());
            dataRepository.CreateOrUpdateTask(taskDTO, project.ProjectId);
            Assert.AreEqual(oldTaskCount + 1, dataRepository.GetAllTasks().Count());
        }
    }
}
